package com.gildedrose;


import com.gildedrose.item.*;
import com.gildedrose.vo.Quality;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

public class GildedRoseTest {
    ExceptedResult expectedResult = new ExceptedResult();

    @Before
    public void setup() throws IOException {
        try (BufferedReader isr
                     = new BufferedReader(new InputStreamReader(GildedRoseTest.class.getResourceAsStream("expected_result.txt")))) {
            String line;
            while ((line = isr.readLine()) != null) {
                if ("".equals(line.trim()) || line.equals("name, sellIn, quality") || line.equals("OMGHAI!")) {
                    continue;
                }
                // sb.append(line).append("\r\n");
                if (line.contains("---") && line.contains("day")) {
                    DayInfo di = new DayInfo();
                    expectedResult.dayInfos.add(di);
                } else {
                    expectedResult.getLastDayInfo().itemInfos.add(line);
                }
            }
        }
    }

    @Test
    public void foo() {
        ItemAdapter[] items = new ItemAdapter[]{
                new DexterityVest(10, Quality.create(20)), // 0
                new AgedBrie(2, Quality.create(0)), // 1
                new Elixir(5, Quality.create(7)), // 2
                new Sulfuras(0), // 3
                new Sulfuras(-1), // 4
                new Backstage(15, Quality.create(20)), // 5
                new Backstage(10, Quality.create(49)), // 6
                new Backstage(5, Quality.create(49)), // 7
                // this conjured item does not work properly yet
                new Conjured(3, Quality.create(6))// 8
        };

        GildedRose app = new GildedRose(items);
        for (int i = 1; i <=99; i++) {
            app.updateQuality();
            for (int j = 0; j < items.length; j++) {
                Assert.assertEquals(expectedResult.getItemByDayIndex(i, j), items[j].toString());
            }
        }

    }

    public static class DayInfo {
        List<String> itemInfos = new ArrayList<>();
    }

    public static class ExceptedResult {
        List<DayInfo> dayInfos = new ArrayList<>();

        public DayInfo getLastDayInfo() {
            return dayInfos.get(dayInfos.size() - 1);
        }

        public String getItemByDayIndex(int day, int itemIndex) {
            return dayInfos.get(day).itemInfos.get(itemIndex);
        }
    }

}
